package it.nola.soccer;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

/**
 * Questa classe controlla l'attività di editing/creazione della cronaca
 * 
 * @author paolo
 */
public class NoteEdit extends Activity {

	private EditText titleText;
	private EditText notesText;
	private EditText dateText;
	private EditText team1Text;
	private EditText team2Text;
	private EditText assistent1Text;
	private EditText assistent2Text;
	private EditText refereeText;
	private EditText resultText;
	private EditText placeText;
	private Long rowId;
	private DbAdapter mDbHelper;

	public void addCard(View view) {
		if (!controllaGiocatori()) {
			return;
		}
		// Intent i=new Intent(this, TeamEdit.class);
		// i.putExtra(DbConst.players.KEY_CRONACA, rowId);
		// i.putExtra(DbConst.players.KEY_TEAM, 1);
		// i.putExtra(DbConst.players.NAME, team1Text.getText());
		// startActivityForResult(i, SoccerNotes.TEAM_EDIT);
	}

	public void chiudi(View view) {
		setResult(RESULT_OK);
		finish();
	}

	public void addGoal(View view) {
		if (!controllaGiocatori()) {
			return;
		}
		Intent i=new Intent(this, GoalEdit.class);
		i.putExtra(DbConst.players.KEY_CRONACA, rowId);
		i.putExtra(DbConst.HOME_TEAM.toString(), team1Text.getText());
		i.putExtra(DbConst.FOREIGN_TEAM.toString(), team2Text.getText());
		startActivityForResult(i, SoccerNotes.GOAL_EDIT);
	}

	public void addSubst(View view) {
		if (!controllaGiocatori()) {
			return;
		}
		Intent i=new Intent(this, SubstEdit.class);
		i.putExtra(DbConst.players.KEY_CRONACA, rowId);
		i.putExtra(DbConst.HOME_TEAM.toString(), team1Text.getText());
		i.putExtra(DbConst.FOREIGN_TEAM.toString(), team2Text.getText());
		startActivityForResult(i, SoccerNotes.SUBST_EDIT);
	}

	private boolean controllaGiocatori() {
		if (!mDbHelper.areThereAnyPlayer(rowId)) {
			Toast t=Toast.makeText(this, getString(R.string.player_missing_error),
					Toast.LENGTH_LONG);
			t.show();
			return false;
		}
		return true;
	}

	/**
	 * Apre la finestra di gestione della squadra di casa
	 * 
	 * @param view
	 */
	public void editTeam1(View view) {
		if (rowId==null) {
			Toast t=Toast.makeText(this, getString(R.string.note_not_saved_error),
					Toast.LENGTH_LONG);
			t.show();
			return;
		}
		if ("".equals(team1Text.getText().toString().trim())||rowId==null) {
			Toast t=Toast
					.makeText(this, getString(R.string.team1_missing_error), Toast.LENGTH_LONG);
			t.show();
			return;
		}
		Intent i=new Intent(this, TeamEdit.class);
		i.putExtra(DbConst.players.KEY_CRONACA, rowId);
		i.putExtra(DbConst.players.KEY_TEAM, DbConst.HOME_TEAM);
		i.putExtra(DbConst.players.NAME, team1Text.getText().toString());
		startActivityForResult(i, SoccerNotes.TEAM_EDIT);
	}

	/**
	 * Apre la finestra di gestione della squadra in trasferta
	 * 
	 * @param view
	 */
	public void editTeam2(View view) {
		if (rowId==null) {
			Toast t=Toast.makeText(this, getString(R.string.note_not_saved_error),
					Toast.LENGTH_LONG);
			t.show();
			return;
		}
		if ("".equals(team1Text.getText().toString().trim())||rowId==null) {
			Toast t=Toast
					.makeText(this, getString(R.string.team2_missing_error), Toast.LENGTH_LONG);
			t.show();
			return;
		}
		Intent i=new Intent(this, TeamEdit.class);
		i.putExtra(DbConst.players.KEY_CRONACA, rowId);
		i.putExtra(DbConst.players.KEY_TEAM, DbConst.FOREIGN_TEAM);
		i.putExtra(DbConst.players.NAME, team2Text.getText().toString());
		startActivityForResult(i, SoccerNotes.TEAM_EDIT);
	}

	/**
	 * @return restituisce l'identificativo della cronaca su cui sto lavorando
	 */
	public Long getRowId() {
		return rowId;
	}

	/**
	 * Azioni da effettuare alla creazione dell'atività
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTitle(getString(R.string.match));
		mDbHelper=new DbAdapter(this);
		mDbHelper.open();
		setContentView(R.layout.note_edit);
		rowId=savedInstanceState!=null ? savedInstanceState.getLong(DbConst.cronache.KEY_ROWID)
				: null;
		if (rowId==null) {
			Bundle extras=getIntent().getExtras();
			rowId=extras!=null ? extras.getLong(DbConst.cronache.KEY_ROWID) : null;
		}
		prepareFields();
		populateFields();
	}

	/**
	 * Quando l'attività va in pausa bisogna salvare
	 */
	@Override
	protected void onPause() {
		super.onPause();
		saveState();
	}

	/**
	 * al resume recupera i campi
	 */
	@Override
	protected void onResume() {
		super.onResume();
		populateFields();
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putLong(DbConst.cronache.KEY_ROWID, rowId);
	}

	@Override
	protected void onStop() {
		super.onPause();
		saveState();
	}

	private void populateFields() {
		if (rowId!=null) { // recupero dal db
			Cursor cc=mDbHelper.fetchNote(rowId);
			startManagingCursor(cc);
			titleText.setText(cc.getString(cc.getColumnIndex(DbConst.cronache.KEY_TITLE)));
			notesText.setText(cc.getString(cc.getColumnIndex(DbConst.cronache.KEY_BODY)));
			dateText.setText(cc.getString(cc.getColumnIndex(DbConst.cronache.KEY_MATCH_DATE)));
			team1Text.setText(cc.getString(cc.getColumnIndex(DbConst.cronache.KEY_TEAM1)));
			team2Text.setText(cc.getString(cc.getColumnIndex(DbConst.cronache.KEY_TEAM2)));
			assistent1Text
					.setText(cc.getString(cc.getColumnIndex(DbConst.cronache.KEY_ASSISTENT1)));
			assistent2Text
					.setText(cc.getString(cc.getColumnIndex(DbConst.cronache.KEY_ASSISTENT2)));
			refereeText.setText(cc.getString(cc.getColumnIndex(DbConst.cronache.KEY_REFEREE)));
			resultText.setText(cc.getString(cc.getColumnIndex(DbConst.cronache.KEY_RESULT)));
			placeText.setText(cc.getString(cc.getColumnIndex(DbConst.cronache.KEY_PLACE)));
		}
		else { // default
			titleText.setText(getString(R.string.default_title));
			notesText.setText(getString(R.string.default_notes));
			placeText.setText(getString(R.string.stadium));
			resultText.setText("0-0");
		}
	}

	/**
	 * Inizializza i campi presenti nella View
	 */
	private void prepareFields() {
		titleText=(EditText) findViewById(R.id.title);
		notesText=(EditText) findViewById(R.id.body);
		dateText=(EditText) findViewById(R.id.match_date);
		assistent1Text=(EditText) findViewById(R.id.assistent1);
		assistent2Text=(EditText) findViewById(R.id.assistent2);
		refereeText=(EditText) findViewById(R.id.referee);
		resultText=(EditText) findViewById(R.id.result);
		placeText=(EditText) findViewById(R.id.place);
		team1Text=(EditText) findViewById(R.id.team1);
		team2Text=(EditText) findViewById(R.id.team2);
	}

	public void salva(View view) {
		saveState();
	}

	private void saveState() {
		String title=titleText.getText().toString();
		String notes=notesText.getText().toString();
		String date=dateText.getText().toString();
		String team1=team1Text.getText().toString();
		String team2=team2Text.getText().toString();
		String assistent1=assistent1Text.getText().toString();
		String assistent2=assistent2Text.getText().toString();
		String referee=refereeText.getText().toString();
		String result=resultText.getText().toString();
		String place=placeText.getText().toString();
		// Se è un inserimento
		if (rowId==null) {
			long id=mDbHelper.createNote(title, notes, team1, team2, date, assistent1, assistent2,
					referee, result, place);
			if (id>0) {
				rowId=id;
			}
		}
		else {
			mDbHelper.updateNote(rowId, title, notes, date, team1, team2, assistent1, assistent2,
					referee, result, place);
		}
	}

}
